library(ggmap)
## Loading required package: ggplot2
library(ggplot2)

library(gganimate)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
crimes <- read.csv("Crimes.csv", header = TRUE,stringsAsFactors=FALSE)
crimes <- crimes[,c(1,2,4:7,10,11)]
#crimes <- data.frame(crimes$IncidntNum,crimes$Category,crimes$DayOfWeek,crimes$Date,crimes$Time,crimes$PdDistrict,crimes$X, crimes$Y)
map <- get_map(location = c(lon = -122.417714, lat=37.794124), zoom = 15, maptype = "terrain")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=37.794124,-122.417714&zoom=15&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
assault <- crimes[crimes$Category == "ASSAULT",]
thief <- crimes[crimes$Category == "BURGLARY",]
vandalism <- crimes[crimes$Category == "VANDALISM",]
intoxication <- crimes[crimes$Category == "DRUNKNESS" | crimes$Category == "DRUG/NARCOTIC",]

plot(map)

ggmap(map) + 
geom_point(aes(x=assault$X, y=assault$Y, show_guide = TRUE, colour="assault"),
           data=assault, alpha=.6, na.rm = T) +
geom_point(aes(x=thief$X, y=thief$Y, show_guide = TRUE, colour="thief"),
     data=thief, alpha=.5, na.rm = T) +
geom_point(aes(x=intoxication$X, y=intoxication$Y, show_guide = TRUE, colour="intoxication"), 
        data=intoxication, alpha=.5, na.rm = T)  + scale_color_manual(values = c("assault"="red" , "thief"= "cyan3","intoxication"= "purple"))
## Warning: Ignoring unknown aesthetics: show_guide

## Warning: Ignoring unknown aesthetics: show_guide

## Warning: Ignoring unknown aesthetics: show_guide

crimePoint <- assault[assault$X > -122.419633 & assault$X < -122.416469 & assault$Y > 37.786311 & assault$Y <37.802016,]
crimeThief <- thief[thief$X > -122.419633 & thief$X < -122.416469 & thief$Y > 37.786311 & thief$Y <37.802016,]
crimeDrunk <- intoxication[intoxication$X> -122.419633 & intoxication$X < -122.416469 & intoxication$Y > 37.786311 & intoxication$Y <37.802016,]

ggmap(map)  + 
          geom_point(aes(x=crimeThief$X, y=crimeThief$Y, show_guide = TRUE, colour="thief"),
           data=crimeThief, alpha=.6, na.rm = T) +
  geom_point(aes(x=crimePoint$X, y=crimePoint$Y, show_guide = TRUE, colour="assault"),
           data=crimePoint, alpha=.6, na.rm = T) +
   geom_point(aes(x=crimeDrunk$X, y=crimeDrunk$Y, show_guide = TRUE, colour="intoxication"),
           data=crimeDrunk, alpha=.6, na.rm = T)
## Warning: Ignoring unknown aesthetics: show_guide

## Warning: Ignoring unknown aesthetics: show_guide

## Warning: Ignoring unknown aesthetics: show_guide

crime.anim <- rbind(crimePoint,crimeThief)
crime.anim <- rbind(crime.anim,crimeDrunk)
crime.anim <- crime.anim[,c(2,7,8)]
crime.anim$step <- -122.419633
crime.anim$alpha <- 0

c.default <- crime.anim

y <- function(x){
  slope <- (37.786311-37.802016)/(-122.416469 +122.419633)
  return(slope*(x+122.416469)+37.786311)
}

for(x in seq(from=-122.419633, to=-122.416469, length.out=100)){
  new.crime <- c.default
  new.crime$step <- x
  new.crime$alpha[new.crime$Y >= y(x)] = 1
  new.crime <- rbind(new.crime, c("WALK",  x, y(x), x,1))
  crime.anim <- rbind(crime.anim, new.crime)
}
crime.anim$X = as.numeric(crime.anim$X)
crime.anim$Y = as.numeric(crime.anim$Y)
crime.anim$Category[crime.anim$Category == "ASSAULT"] = "red"
crime.anim$Category[crime.anim$Category == "BURGLARY"] = "cyan3"
crime.anim$Category[crime.anim$Category == "DRUG/NARCOTIC"] = "purple"
crime.anim$Category[crime.anim$Category == "WALK"] = "black"
p <- ggmap(map)
suppressWarnings(p <- p +
                   geom_point(aes(X, Y, show_guide = TRUE, frame=step), data=crime.anim, alpha=crime.anim$alpha,color=crime.anim$Category, na.rm = T) 
              #+ scale_color_manual(values = c("ASSAULT"="red" , "BURGLARY"= "cyan3","DRUG/NARCOTIC"= "purple", "WALK"="black"))
)
knitr::opts_chunk$set(message = FALSE, warning = FALSE, fig.show = "animate")
gganimate(p, filename = "animation.gif", title_frame = FALSE, interval=0.15)
## Executing: 
## convert -loop 0 -delay 15 Rplot1.png Rplot2.png Rplot3.png
##     Rplot4.png Rplot5.png Rplot6.png Rplot7.png Rplot8.png
##     Rplot9.png Rplot10.png Rplot11.png Rplot12.png Rplot13.png
##     Rplot14.png Rplot15.png Rplot16.png Rplot17.png Rplot18.png
##     Rplot19.png Rplot20.png Rplot21.png Rplot22.png Rplot23.png
##     Rplot24.png Rplot25.png Rplot26.png Rplot27.png Rplot28.png
##     Rplot29.png Rplot30.png Rplot31.png Rplot32.png Rplot33.png
##     Rplot34.png Rplot35.png Rplot36.png Rplot37.png Rplot38.png
##     Rplot39.png Rplot40.png Rplot41.png Rplot42.png Rplot43.png
##     Rplot44.png Rplot45.png Rplot46.png Rplot47.png Rplot48.png
##     Rplot49.png Rplot50.png Rplot51.png Rplot52.png Rplot53.png
##     Rplot54.png Rplot55.png Rplot56.png Rplot57.png Rplot58.png
##     Rplot59.png Rplot60.png Rplot61.png Rplot62.png Rplot63.png
##     Rplot64.png Rplot65.png Rplot66.png Rplot67.png Rplot68.png
##     Rplot69.png Rplot70.png Rplot71.png Rplot72.png Rplot73.png
##     Rplot74.png Rplot75.png Rplot76.png Rplot77.png Rplot78.png
##     Rplot79.png Rplot80.png Rplot81.png Rplot82.png Rplot83.png
##     Rplot84.png Rplot85.png Rplot86.png Rplot87.png Rplot88.png
##     Rplot89.png Rplot90.png Rplot91.png Rplot92.png Rplot93.png
##     Rplot94.png Rplot95.png Rplot96.png Rplot97.png Rplot98.png
##     Rplot99.png Rplot100.png 'animation.gif'
## Output at: animation.gif
# anim <- crime.anim %>% arrange(step) %>%
#   select(X, Y, Category, alpha) %>%
#   rename(x=X, y=Y, )
# anim.tween <- tween_elements(anim,)